﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using Facade;
using IronOne.SalesOne.BusinessObjects;
using System.Text.RegularExpressions;

namespace IronOne.SalesOne.SalesOneWeb.BatchJobs
{
    public partial class Email : SOSecurityPage
    {


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ConfiguraionFacade cf = new ConfiguraionFacade();

                ddlJobList.DataSource = cf.GetAvailableMailJobs();
                ddlJobList.DataBind();

                details.Visible = false;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (isDataValid(txtEmail.Text))
            {
                String jobId = (ddlJobList.SelectedValue).ToString();
                String email = txtEmail.Text;
                int? days = null;
                if (!string.IsNullOrEmpty(txtNumberOfDays.Text))
                    days = Convert.ToInt32((txtNumberOfDays.Text));

                Save(jobId, email, days);
            }
            else
            {
                MassageSummery("IIS501");
            }

        }

        private bool isDataValid(string p)
        {
            foreach (string mail in p.Split(';'))
            {
                if (!isEmailValid(mail))
                    return false;
            }
            return true;
        }

        private bool isEmailValid(string mail)
        {
            Regex emailPattern=new Regex("^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$");
            return emailPattern.IsMatch(mail);          
        }

        public void Save(String JobId, String email, int? days)
        {

            ConfiguraionFacade cf = new ConfiguraionFacade();
            Emails emails = new Emails();

            emails.JobId = JobId;
            emails.Email = email;
            
            emails.NumberOfDays = days;


            int successInsert = cf.InsertEmails(emails);

            if (successInsert == 1)
            {
                /*txtEmail.Text = "";
                txtJobId.Text = "";
                txtNumberOfDays.Text = "";*/
                MassageSummery("IIS401");

            } if (successInsert == 0)
            {
                MassageSummery("IIS502");
            }

        }

        protected void ddlJobList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlJobList.SelectedIndex > 0)
            {
                details.Visible = true;

                ConfiguraionFacade cf = new ConfiguraionFacade();
                Emails mail = cf.getJobInfo(ddlJobList.SelectedValue);

                txtEmail.Text = mail.Email;
                if (ddlJobList.SelectedValue == "INT001_COLLECTION_UNPROCESSED" || ddlJobList.SelectedValue == "INT002_COLLECTION_NOTSYNC_3WKDAY")
                {
                    txtNumberOfDays_NumericUpDownExtender.Enabled = true;
                    txtNumberOfDays.Enabled = true;
                    txtNumberOfDays.Text = mail.NumberOfDays.ToString();
                }
                else
                {
                    txtNumberOfDays.Text = "";
                    txtNumberOfDays.Enabled = false;
                    txtNumberOfDays_NumericUpDownExtender.Enabled = false;
                }
            }
            else
            {
                details.Visible = false;
            }
        }

    }
}
